<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta charset="UTF-8">
    <title>业务员修改</title>
    <meta content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0, shrink-to-fit=no' name='viewport' />
    <link rel="stylesheet" href="assets/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i">
    <link rel="stylesheet" href="assets/css/ready.css">
</head>
<body>
<div class="wrapper">
    <div class="main-header">
        <div class="logo-header">
            <a href="enterProduct.html" class="logo">
                艾尔之光仓库管理
            </a>
            <button class="navbar-toggler sidenav-toggler ml-auto" type="button" data-toggle="collapse" data-target="collapse" aria-controls="sidebar" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <button class="topbar-toggler more"><i class="la la-ellipsis-v"></i></button>
        </div>
    </div>
    <div class="sidebar">
        <div class="scrollbar-inner sidebar-wrapper">
            <ul class="nav">
                <li class="nav-item">
                    <a href="enterProduct.html">
                        <i class="la la-th"></i>
                        <p>商品录入</p>
                    </a>
                </li>
                <li class="nav-item">
                    <a href="enterInventory.html">
                        <i class="la la-th"></i>
                        <p>库存录入</p>
                    </a>
                </li>
                <li class="nav-item">
                    <a href="enterPurchase.html">
                        <i class="la la-th"></i>
                        <p>采购预录</p>
                    </a>
                </li>
                <li class="nav-item">
                    <a href="enterGoodsArrived.html">
                        <i class="la la-th"></i>
                        <p>采购到货录入</p>
                    </a>
                </li>
                <li class="nav-item">
                    <a href="export.html">
                        <i class="la la-th"></i>
                        <p>导出Execl</p>
                    </a>
                </li>
                <li class="nav-item active">
                    <a href="updateSalesperson.html">
                        <i class="la la-th"></i>
                        <p>修改业务员姓名</p>
                    </a>
                </li>
            </ul>
        </div>
    </div>
    <div class="main-panel">
        <div class="content">
            <div class="container-fluid">
                <div class="card">
                    <div class="card-header">
                        <div class="card-title">业务员修改</div>
                    </div>
                    <div class="card-body">
                        <div class="form-group form-inline">
                            <label for="modelName" class="col-md-3 col-form-label">型号</label>
                            <div class="col-md-9 p-0">
                                <input type="text" list="modeNameList" class="form-control input-full" id="modelName" placeholder="请输入商品型号">
                                <datalist id="modeNameList">
                                </datalist>
                            </div>
                        </div>
                        <div class="form-group form-inline">
                            <label for="oldSalesperson" class="col-md-3 col-form-label">旧业务员</label>
                            <div class="col-md-9 p-0">
                                <input type="text" list="oldSalespersonList" class="form-control input-full" id="oldSalesperson" placeholder="请输入旧业务员姓名">
                                <datalist id="oldSalespersonList">
                                </datalist>
                            </div>
                        </div>
                        <div class="form-group form-inline">
                            <label for="newSalesperson" class="col-md-3 col-form-label">新业务员</label>
                            <div class="col-md-9 p-0">
                                <input type="text" class="form-control input-full" id="newSalesperson" placeholder="请输入新业务员姓名">
                            </div>
                        </div>
                    </div>
                    <div class="card-action">
                        <button id="updateSalesperson" class="btn btn-success">修改</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script src="assets/js/core/jquery.3.2.1.min.js"></script>
<script src="assets/js/plugin/jquery-ui-1.12.1.custom/jquery-ui.min.js"></script>
<script src="assets/js/core/popper.min.js"></script>
<script src="assets/js/core/bootstrap.min.js"></script>
<script src="assets/js/plugin/chartist/chartist.min.js"></script>
<script src="assets/js/plugin/chartist/plugin/chartist-plugin-tooltip.min.js"></script>
<script src="assets/js/plugin/bootstrap-notify/bootstrap-notify.min.js"></script>
<script src="assets/js/plugin/bootstrap-toggle/bootstrap-toggle.min.js"></script>
<script src="assets/js/plugin/jquery-mapael/jquery.mapael.min.js"></script>
<script src="assets/js/plugin/jquery-mapael/maps/world_countries.min.js"></script>
<script src="assets/js/plugin/chart-circle/circles.min.js"></script>
<script src="assets/js/plugin/jquery-scrollbar/jquery.scrollbar.min.js"></script>
<script src="assets/js/ready.min.js"></script>
<script>
    $.ajax({
        url: "/model_name_list",
        type: 'post',
        async: false,   //如果不加，无法实现数据传值
        dataType: 'json',
        success: function (datajson) {
            var result = datajson;
            if (result.code == "0000"){
                var data = result.data
                $(data).each(function (key) {  //循环读取后台传来的Json数据
                    AppendNode(data[key], data[key]);   //将value和text添加到下拉框中
                });
            }else {
                alert(result.message)
            }
        },
        error: function () {
            alert("出现错误");
        }
    });
    function AppendNode(value, text) {
        $("#modeNameList").append("<option value='" + value + "'>" + text + "</option>");
    }

    $.ajax({
        url: "/get_all_salesperson",
        type: 'post',
        async: false,   //如果不加，无法实现数据传值
        dataType: 'json',
        success: function (datajson) {
            var result = datajson;
            console.log(result);
            if (result.code == "0000"){
                var data = result.data
                $(data).each(function (key) {  //循环读取后台传来的Json数据
                    AppendSalespersonNode(data[key], data[key]);   //将value和text添加到下拉框中
                });
            }else {
                alert(result.message)
            }
        },
        error: function () {
            alert("出现错误");
        }
    });
    function AppendSalespersonNode(value, text) {
        $("#oldSalespersonList").append("<option value='" + value + "'>" + text + "</option>");
    }


    $("#updateSalesperson").click(function (){
        var modelName = $("#modelName").val();
        var oldSalesperson = $("#oldSalesperson").val();
        var newSalesperson = $("#newSalesperson").val();
        var json = {
            "modelName":modelName,
            "oldSalesperson":oldSalesperson,
            "newSalesperson":newSalesperson
        };
        if (!modelName){
            alert("请输入型号");
            throw new Error("型号输入异常");
        }
        if (!oldSalesperson){
            alert("请输入旧业务员名字");
            throw new Error("旧业务员名字输入异常");
        }
        if (!newSalesperson){
            alert("请输入新业务员名字");
            throw new Error("新业务员名字输入异常");
        }
        if (oldSalesperson == newSalesperson){
            alert("旧业务员名字与新业务员名字一致");
            throw new Error("旧业务员名字与新业务员名字一致");
        }
        json = JSON.stringify(json)//转化为json字符串
        $.ajax({
            url: "/update_salesperson",
            headers:{
                'Content-Type': 'application/json' // 确保这里的 Content-Type 与服务器端期望的格式一致
            },
            type: 'post',
            data: json,
            async: false,   //如果不加，无法实现数据传值
            dataType: 'json',
            success: function (datajson) {
                console.log(datajson)
                alert(datajson["message"])
            },
            error: function (){
                alert("录入失败")
            }
        });

    });
</script>
</html>